package com.orhon.smartcampus.modules.student.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLGrades;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.GradeMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.dao.EclassDao;
import com.orhon.smartcampus.modules.student.entity.Arrives;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.student.entity.EclassRecords;
import com.orhon.smartcampus.modules.student.entity.SInformation;
import com.orhon.smartcampus.modules.student.gqlModel.GQLArrives;
import com.orhon.smartcampus.modules.student.gqlModel.GQLEclass;
import com.orhon.smartcampus.modules.student.gqlModel.GQLEclassRecords;
import com.orhon.smartcampus.modules.student.gqlModel.GQLStudent;
import com.orhon.smartcampus.modules.student.mm.ArrivesMapper;
import com.orhon.smartcampus.modules.student.mm.EclassMapper;
import com.orhon.smartcampus.modules.student.mm.EclassRecordssMapper;
import com.orhon.smartcampus.modules.student.mm.StudentMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Component
public class RegionEclassesFetcher implements DataFetcher {
	@Autowired
	EclassDao eclassDao;

	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

//		String token = tokenService.getSystemToken();
//		if(token==null) {
//			return  R.NoLogin();
//		}

		List<GQLEclass> GQLEclass = new ArrayList<>();


		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from Eclass where deleted_at = null ";

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("id")!=null) {
				hql += "and id" + Util.decide(map.get("id"));
			}
			if(map.get("school_id")!=null) {
				hql += "and school_id" + Util.decide(map.get("school_id"));
			}
			if(map.get("grade_id")!=null) {
				hql += "and grade_id " + Util.decide(map.get("grade_id"));
			}
			if(map.get("arrive_id")!=null) {
				hql += "and arrives_id" + Util.decide(map.get("arrive_id"));
			}
			if(map.get("code")!=null) {
				hql += "and eclass_code " + Util.decide(map.get("code"));
			}
			if(map.get("status")!=null) {
				hql += "and class_status" + Util.decide(map.get("status"));
			}
			if(map.get("kind_dict")!=null) {
				hql += "and class_kind_dict " + Util.decide(map.get("kind_dict"));
			}
			if(map.get("nature")!=null) {
				hql += "and class_nature" + Util.decide(map.get("nature"));
			}
			if(map.get("building_id")!=null) {
				hql += "and buildings_id " + Util.decide(map.get("building_id"));
			}
			if(map.get("unit_id")!=null) {
				hql += "and unit_id " + Util.decide(map.get("unit_id"));
			}
			if(map.get("floor_id")!=null) {
				hql += "and floor_id " + Util.decide(map.get("floor_id"));
			}
			if(map.get("classroom_id")!=null) {
				hql += "and classroom " + Util.decide(map.get("classroom_id"));
			}
			if(map.get("graduated_flag")!=null) {
				hql += "and graduated_flag " + Util.decide(map.get("graduated_flag"));
			}
			if(map.get("eclass_number")!=null) {
				hql += "and eclass_number " + Util.decide(map.get("eclass_number"));
			}

		}

		List<Eclass> list = eclassDao.find(hql,null,page.getPage(),page.getLimit());

		for (Eclass item : list) {
			GQLGrades gqlGrades = null;
			if (env.getSelectionSet().contains("data/grade")){
				Grades grades = item.getBaseGradesByGradeId();
				gqlGrades = GradeMapper.MAPPER.toGrade(grades);
			}

			GQLArrives gqlArrives = null;
			if (env.getSelectionSet().contains("data/arrives")){
				Arrives arrives = item.getStudentArrivesByArrivesId();
				gqlArrives = ArrivesMapper.MAPPER.toArrives(arrives);
			}

			GQLSchool gqlSchool = null;
			if (env.getSelectionSet().contains("data/school")){
				Schools schools = item.getBaseSchoolsBySchoolId();
				gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
			}

			List<GQLEclassRecords> GQLEclassRecords = new ArrayList<>();
			if (env.getSelectionSet().contains("data/students")){
				List<EclassRecords> eclassRecords = item.getStudents();
				for (EclassRecords records :eclassRecords){
					SInformation student = records.getStudent();
					GQLStudent gqlStudent = StudentMapper.MAPPER.toStudent(student);
					GQLEclassRecords gqlEclassRecord = EclassRecordssMapper.MAPPER.toEclassRecords(records);
					gqlEclassRecord.setStudent(gqlStudent);
					Users users = records.getUsers();
					GQLUser gqlUser = UserMapper.MAPPER.mapUser(users);
					gqlEclassRecord.setUsers(gqlUser);
					GQLEclassRecords.add(gqlEclassRecord);
				}
			}
			GQLEclass eclass = EclassMapper.MAPPER.toEclass(item);
			eclass.setGrade(gqlGrades);
			eclass.setArrives(gqlArrives);
			eclass.setSchool(gqlSchool);
			eclass.setStudents(GQLEclassRecords);
			GQLEclass.add(eclass);
		}

		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = eclassDao.getTotalCount(hql);
		}
		return Result.ok().put("data", GQLEclass).put("total", total);
	}


}
